package com.ca.project.Server.Modules.SuperUser.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ca.project.Server.Modules.SuperUser.Context.SuperUserLoginContext;
import com.ca.project.Server.Modules.SuperUser.Context.SuperUserRegisterContext;
import com.ca.project.Server.Modules.SuperUser.Converter.SuperUserConverter;
import com.ca.project.Server.Modules.SuperUser.Entity.Superuser;
import com.ca.project.Server.Modules.SuperUser.service.SuperuserService;
import com.ca.project.Server.Modules.SuperUser.Mapper.SuperuserMapper;
import com.ca.project.core.Exception.BusinessException;
import com.ca.project.core.Respond.ResponseCode;
import com.ca.project.core.Util.IdUtil;
import com.ca.project.core.Util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author ASUS
 * @description 针对表【superuser】的数据库操作Service实现
 * @createDate 2023-10-04 23:07:24
 */
@Service
public class SuperuserServiceImpl extends ServiceImpl<SuperuserMapper, Superuser> implements SuperuserService {

    @Autowired
    private SuperUserConverter superUserConverter;

    /**
     * 管理员登录业务
     * <p>
     * 1、管理员登陆信信息校验
     *
     * @param superUserLoginContext
     * @return
     */
    @Override
    public String login(SuperUserLoginContext superUserLoginContext) {
        return checkLoginInfo(superUserLoginContext);
    }

    /**
     * 管路员注册业务
     * <p>
     * 需要实现的功能点
     * 1、注册管理员信息
     * 2、创建管理员根本目录信息
     * <p>
     * 需要实现的技术难点
     * 1.该业务是冥等的
     *
     * @param userRegisterContext
     * @return
     */
    @Override
    public Superuser register(SuperUserRegisterContext userRegisterContext) {
        assembleUserEntity(userRegisterContext);
        doRegister(userRegisterContext);
        return userRegisterContext.getEntity();
    }

    /***********************************************private****************************************************************/

    /**
     * 实体转换
     * 由上下文信息转化成用户实体，封装进上下文
     *
     * @param userRegisterContext
     */
    private void assembleUserEntity(SuperUserRegisterContext userRegisterContext) {
        Superuser entity = superUserConverter.superUserRegisterPO2SuperUser(userRegisterContext);
        String salt = PasswordUtil.getSalt(),
                dbPassword = PasswordUtil.encryptPassword(salt, userRegisterContext.getPassword());
        entity.setUserId(IdUtil.get());
        entity.setSalt(salt);
        entity.setUsername(userRegisterContext.getUsername());
        entity.setPassword(dbPassword);
        userRegisterContext.setEntity(entity);
    }

    /**
     * 创建根目录信息
     *
     * @param userRegisterContext
     */
    private void doRegister(SuperUserRegisterContext userRegisterContext) {
        Superuser entity = userRegisterContext.getEntity();
        if (Objects.isNull(entity)) {
            try {
                if (!save(entity))
                    throw new BusinessException("注册失败");
            } catch (DuplicateKeyException duException) {
                throw new BusinessException("该用户已存在");
            }
            return;
        }
        throw new BusinessException(ResponseCode.ERROR);
    }

    /**
     * 校验用户名和密码
     *
     * @param superUserLoginContext
     * @return
     */
    private String checkLoginInfo(SuperUserLoginContext superUserLoginContext) {
        String username = superUserLoginContext.getUsername();
        String password = superUserLoginContext.getPassword();

        Superuser entity = getSuperUserByUsername(username);
        if (Objects.isNull(entity))
            throw new BusinessException("该用户不存在");

        String salt = PasswordUtil.getSalt();
        String encPassword = PasswordUtil.encryptPassword(salt, password);
        String dbPassword = entity.getPassword();
        if (!Objects.equals(encPassword, dbPassword))
            throw new BusinessException("密码不正确");
        return null;
    }

    /**
     * 通过用户名称获取用户实体信息
     *
     * @param username
     * @return
     */
    private Superuser getSuperUserByUsername(String username) {
        QueryWrapper<Superuser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return getOne(wrapper);
    }

}




